package com.smartguard.action;

import java.io.IOException;
import java.io.PrintWriter;

import net.sf.json.util.JSONUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartguard.bean.DeviceEvent;
import com.smartguard.bean.User;
import com.smartguard.common.Const;
import com.smartguard.service.RuleService;
import com.smartguard.util.JSONUtil;

public class RuleAction extends BaseAction {

	private DeviceEvent de;
	private RuleService ruleService;

	public RuleAction() {
		this.ruleService = new RuleService();
	}

	public void addRule() throws IOException {

		User user = (User) this.getSession().get(Const.SESSION_USER);
		PrintWriter out = this.getWriter();

		if (user == null) {
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			out.write(json.toJSONString());
			return;
		}
		if (ruleService.findOneRule(de) != null) {
			JSONObject json = JSONUtil.newErrMsgJSONObject("不要重复添加事件规则");
			out.write(json.toJSONString());
			return;
		}
		if (ruleService.insertRule(de)) {
			JSONObject json = JSONUtil.newSuccessJSONObject();
			out.write(json.toJSONString());
		}
		out.flush();
		out.close();

	}

	public String deleteRule() {
		
		return "deleteSuccess";
	}

	public String modifyRule() {

		return "modifySuccess";
	}

	
	public DeviceEvent getDe() {
		return de;
	}

	public void setDe(DeviceEvent de) {
		this.de = de;
	}

}
